package torres.item;

import torres.entity.Entity;
import torres.fileIO.BasicContainer;
import torres.fileIO.ClassContainer;

import java.util.ArrayList;
import java.util.List;
import torres.action.Action;

/**
 * @author Torres
 * Represents an item that an Entity can pick up off the map. 
 * Maintains a list of Actions that occur when that item is used.
 */

public class TakeableItem extends Item
{
	private List<Action> actions;
	private int itemCost;
	
	/**
	 * Creates a new TakeableItem with the specified name.
	 * @param name The name of the TakeableItem.
	 * @param id ID of TakeableItem
	 * @param itemCost Value of TakeableItem
	 */
	public TakeableItem(String name, int id, int itemCost) {
		super(name, id);
		actions = new ArrayList<Action>();
		this.itemCost = itemCost;
		//this.imageName = "sword.png";
	}
	
	
	/**
	 * Creates a new TakeableItem with the specified name and action list.
	 * @param name The name of the TakeableItem.
	 * @param id ID of TakeableItem
	 * @param actions The actions that should occur on use.
	 * @param itemCost Value of TakeableItem
	 */
	public TakeableItem(String name, int id, List<Action> actions, int itemCost) {
		this(name, id, itemCost);
		this.actions = actions;
		//this.imageName = "sword.png";
	}
	
	/**
	 * Creates a new instance of TakeableItem that is a copy of the specified TakeableItem 
	 * @param item The item to be copied.
	 */
	public TakeableItem(TakeableItem item) {
		this(item.getName(), item.getID(), item.getActions(), item.getItemCost());
	}
	
	/**
	 * Triggers the action of an Entity walking on this item.
	 * @param e The Entity that is walking on this item
	 */
	public void onTouch(Entity entity) {
		if(entity.addItem(this))
			setConsumed();
	}

	
	/**
	 * Uses the item.
	 */
	public void use(Entity entity) {
		entity.useItem(this);
	}
	
	/**
	 * Applies actions of item to specified entity 
	 * @param entity Entity actions are applied to
	 */
	public void apply(Entity entity) {
		for(Action a : this.getActions()) {
			a.setUp(entity);
			Thread t = new Thread(a);
			t.setName("TakeItem" + t.getId());
			t.start();
		}
	}
	
	/**
	 * Get the list of Actions that occur on use.
	 * @return The list of Actions that occur on use.
	 */
	private List<Action> getActions() {
		return this.actions;
	}
	
	/**
	 * Add the specified action to the Item.
	 * @param action The action to be added. 
	 */
	public void addAction(Action action) {
		
		actions.add(action);
	}
	
	/**
	 * Set the actions of this item to the specified list.
	 * @param actions The new list of Actions.
	 */
	public void setActionList(List<Action> actions)
	{
		this.actions = actions;
	}
	
	/**
	 * Appends all of the actions in the specified list to this item's action list.
	 * @param actions The list of Actions to be added.
	 */
	public void appendActionList(List<Action> actions)
	{
		this.actions.addAll(actions);
	}
	
	/**
	 * Returns the item's information 
	 * @return information about item
	 */
	public String getToolTip() {
		String ret="<html>Name: "+super.getName()+"<br>";
		ret += "</html>";
		return ret;
	}


	/* (non-Javadoc)
	 * @see torres.item.Item#save()
	 */
	@Override
	public ClassContainer save() {
		ClassContainer curClass = super.save();
		curClass.addProperty(new BasicContainer("type", "TakeableItem"));
		
		return curClass;
		
	}

/*
	/* (non-Javadoc)
	 * @see torres.item.Item#load(torres.fileIO.ClassContainer)
	 */
	@Override
	public void load(ClassContainer classContainer) {
		// TODO Auto-generated method stub
		
	}
	
	/**
	 * Returns cost of item
	 * @return Item Cost
	 */
	public int getItemCost()
	{
		return itemCost;
	}
	
	/**
	 * Sets Item's new cost
	 * @param int itemCost for Item Cost
	 */
	public void setItemCost(int itemCost)
	{
		if(itemCost < 0)
			itemCost = 0; 
		this.itemCost = itemCost;
	}


	public void use(Entity player, Entity interactedWith) {
		player.removeItem(this);
		apply(interactedWith);
	}
}
